package com.ngc.mall.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.SpecKeyDomain;
import com.ngc.mall.domain.SpecValueDomain;
import com.ngc.mall.dto.SpecDTO;
import com.ngc.mall.dto.SpecValueDTO;
import com.ngc.mall.param.SpecParam;
import com.ngc.mall.param.SpecValueParam;
import com.ngc.mall.service.ISpecService;
import com.ngc.mall.service.ISpecValueService;
import com.ngc.mall.vo.SpecDetailVO;
import com.ngc.mall.vo.SpecValueVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 规格
 *
 * @author: chenxiaolin
 * @date 2023/4/4 8:55
**/
@RestController
@RequestMapping("/spec")
@Api(tags = "规格")
public class SpecController {
    @Resource
    private ISpecService service;
    @Resource
    private ISpecValueService iSpecValueService;

    @GetMapping("/list")
    @ApiOperation("获取所有规格名")
    public ResponseResult<List<SpecDetailVO>> selectAllSpec(){
        return ResponseResult.success(this.service.selectAllSpec());
    }


    @GetMapping("/key/all/{id}")
    @ApiOperation("获取指定规格名id的所有规格值信息")
    public ResponseResult<List<SpecValueVO>> selectAllValueByKeyId(@PathVariable Long id){
        return ResponseResult.success(iSpecValueService.selectAllValueByKeyId(id));
    }


    @PostMapping("")
    @ApiOperation("新建规格名")
    public ResponseResult<Long> createSpec(@RequestBody SpecParam param){
        SpecDTO dto=SpecDTO
                .builder()
                .name(param.getName())
                .unit(param.getUnit())
                .note(param.getNote())
                .build();
        return ResponseResult.success(this.service.createSpec(dto));
    }


    @PutMapping("/key/{id}")
    @ApiOperation("根据id更新规格名")
    public ResponseResult<Boolean> updateSpecById(@PathVariable Long id,@RequestBody SpecParam param){
        return ResponseResult.success(service.update(
                Wrappers.<SpecKeyDomain>lambdaUpdate()
                        .eq(SpecKeyDomain::getId,id)
                        .set(SpecKeyDomain::getNote,param.getNote())
                        .set(SpecKeyDomain::getUnit,param.getUnit())
                )
        );
    }

    /**
     * 根据id删除规格名
     * @param id
     * @return
     */
    @DeleteMapping("/key/{id}")
    public ResponseResult<Boolean> deleteSpec(@PathVariable Long id){
        return ResponseResult.success(this.service.removeById(id));
    }

    /**
     * 新建规格值
     */
    @PostMapping("/value")
    public ResponseResult<Long> createSpecValue(@RequestBody SpecValueParam param){
        SpecValueDTO dto=SpecValueDTO.builder().value(param.getValue()).specId(param.getSpecId()).build();
        return ResponseResult.success(this.iSpecValueService.createSpecValue(dto));
    }

    /**
     * 删除规格值
     */
    @DeleteMapping("/value/{id}")
    public ResponseResult<Boolean> deleteSpecValueById(@PathVariable Long id){
        return ResponseResult.success(this.iSpecValueService.removeById(id));
    }

    /**
     * 更新规格值
     */
    @PutMapping("/value/{id}")
    public ResponseResult<Boolean> updateSpecValueById(@PathVariable Long id,@RequestBody SpecValueParam param){
        return ResponseResult.success(this.iSpecValueService.update(
                Wrappers.<SpecValueDomain>lambdaUpdate().eq(SpecValueDomain::getId,id).set(SpecValueDomain::getValue,param.getValue())
        ));
    }

}
